package com.ruoyi.customerSample.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;

import com.ruoyi.customerSample.domain.SampleTypeBill;
import com.ruoyi.customerSample.domain.SampleTypeDetails;
import com.ruoyi.customerSample.service.ISampleTypeBillService;
import com.ruoyi.customerSample.service.ISampleTypeDetailsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 样品分类保存单据Controller
 * 
 * @author lgq
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/sampleType/bill")
@Api(value="样品分类保存单据",tags = "样品分类保存单据接口")
public class SampleTypeBillController extends BaseController
{
    @Autowired
    private ISampleTypeBillService sampleTypeBillService;

    @Autowired
    private ISampleTypeDetailsService sampleDetailBillService;

    /**
     * 查询样品分类保存单据列表
     */
    @GetMapping("/list")
    @ApiOperation(value="查询样品分类保存单据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(SampleTypeBill sampleTypeBill)
    {
        startPage();
        List<SampleTypeBill> list = sampleTypeBillService.selectSampleTypeBillList(sampleTypeBill);
        //字典转换样品类别
        for(SampleTypeBill bill : list) {
            bill.setSampleType(DictUtils.getDictLabel(bill.getItemType(), bill.getSampleType(), ""));
        }
        return getDataTable(list);
    }

    /**
     * 导出样品分类保存单据列表
     */
    @Log(title = "样品分类保存单据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SampleTypeBill sampleTypeBill) throws IOException {
        List<SampleTypeBill> list = sampleTypeBillService.selectSampleTypeBillList(sampleTypeBill);
        //获取明细数据
        if(list.size() > 0) {
            SampleTypeDetails sampleTypeDetails = new SampleTypeDetails();
            sampleTypeDetails.setSampleTypeId(list.get(0).getId());
            List<SampleTypeDetails> detaillist = sampleDetailBillService.selectSampleTypeDetailsList(sampleTypeDetails);
            //输出导入
            String templateFileName = RuoYiConfig.getProfile() +  "//tem//back.xlsx";
            //String templateFileName = ClassUtils.getDefaultClassLoader().getResource("tem").getPath() + "/" + "back.xlsx";
            String fileName = System.currentTimeMillis() + "_back.xlsx";
            String filePath = RuoYiConfig.getDownloadPath() + fileName;
            ExcelWriter excelWriter = EasyExcel.write(filePath).withTemplate(templateFileName).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行，然后下面的数据往后移动。默认 是false，会直接使用下一行，如果没有则创建。
            // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了，所以慎用
            // 简单的说 如果你的模板有list,且list不是最后一行，下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
            // 如果数据量大 list不是最后一行 参照下一个
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(detaillist, fillConfig, writeSheet);
            Map<String, Object> map = new HashMap<String, Object>();
            //根据key查询值
            map.put("sampleType", DictUtils.getDictLabel(list.get(0).getItemType(), list.get(0).getSampleType(), ""));
            map.put("sampleAdmin", list.get(0).getSampleAdmin());
            excelWriter.fill(map, writeSheet);
            excelWriter.finish();
            return AjaxResult.success(fileName);
        }
        return AjaxResult.error("下载失败！");
    }

    /**
     * 获取样品分类保存单据详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取样品分类保存单据详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sampleTypeBillService.selectSampleTypeBillById(id));
    }

    /**
     * 获取样品分类id获取详细信息
     */
    @ApiOperation(value="获取样品分类id获取详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    @GetMapping(value = "/getInfoByTypeId/{id}")
    public AjaxResult getInfoByTypeId(@PathVariable("id") String id)
    {
        return AjaxResult.success(sampleDetailBillService.selectSampleTypeDetailsByTypeId(id));
    }

    /**
     * 新增样品分类保存单据
     */
    @Log(title = "样品分类保存单据", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增样品分类保存单据")
    public AjaxResult add(@ApiParam(name="样品分类保存单据",value="传入json格式",required=true)@RequestBody SampleTypeBill sampleTypeBill)
    {
        return toAjax(sampleTypeBillService.insertSampleTypeBill(sampleTypeBill));
    }

    /**
     * 修改样品分类保存单据
     */
    @Log(title = "样品分类保存单据", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改样品分类保存单据")
    public AjaxResult edit(@ApiParam(name="样品分类保存单据",value="传入json格式",required=true)@RequestBody SampleTypeBill sampleTypeBill)
    {
        return toAjax(sampleTypeBillService.updateSampleTypeBill(sampleTypeBill));
    }

    /**
     * 删除样品分类保存单据
     */
    @Log(title = "样品分类保存单据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="删除样品分类保存单据")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sampleTypeBillService.deleteSampleTypeBillByIds(ids));
    }
}
